package com.agritech.controller;

import com.agritech.common.Result;
import com.agritech.entity.Question;
import com.agritech.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 问题控制器
 */
@RestController
@CrossOrigin
@RequestMapping("/api/questions")
public class QuestionController {
    
    @Autowired
    private QuestionService questionService;
    
    /**
     * 根据用户ID获取问题列表
     * @param userID 用户ID
     * @return 问题列表
     */
    @GetMapping("/user/{userID}")
    public List<Question> getQuestionsByUserId(@PathVariable Long userID) {
        return questionService.getQuestionsByUserId(userID);
    }
    
    /**
     * 根据ID获取问题详情
     * @param id 问题ID
     * @return 问题对象
     */
    @GetMapping("/{id}")
    public Question getQuestionById(@PathVariable Long id) {
        return questionService.getQuestionById(id);
    }
    
    /**
     * 获取所有问题
     * @return 问题列表
     */
    @GetMapping("/getAll")
    public List<Question> getAllQuestions() {
        return questionService.getAllQuestions();
    }
    
    /**
     * 添加新问题
     * @param question 问题对象
     * @return 添加结果
     */
    @PostMapping("/add")
    public Result addQuestion(@RequestBody Question question) {
        System.out.println("收到问题添加请求: " + question.getQuestionText());
        questionService.addQuestion(question);
        return Result.ok("问题添加成功");
    }

    
    /**
     * 更新问题状态
     * @param question 问题对象
     * @return 更新结果
     */
    @PutMapping("/status")
    public boolean updateQuestionStatus(@RequestBody Question question) {
        return questionService.updateQuestionStatus(question);
    }

    /**
     * 根据 ID 删除问题
     * @param id 问题 ID
     * @return 删除结果
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteQuestionById(@PathVariable Long id) {
        boolean success = questionService.deleteQuestionById(id);
        if (success) {
            return Result.ok("删除成功");
        } else {
            return Result.error("删除失败，问题不存在");
        }
    }


}    